package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.*;

@Service
public class UserServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;


    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectByPhone(phone);
        return count > 0;
    }


    @Override
    public void sendVerifyCode(String phone) {
        //1. 创建验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);

        //2. 把验证码放进redis中(设计redis存储的key-value)
        //String key = "VerifyCode:" + phone;
        String key = RedisKeys.VERIFY_CODE.join(phone);
        userInfoRedisService.setVerifyCode(key, code, Consts.VERIFY_CODE_VAI_TIME);

        //3. 发送验证码
        System.out.println("验证码为:" + code + ",请在" + Consts.VERIFY_CODE_VAI_TIME + "分钟内完成");
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {
        //参数验证是否合法
        AssertUtil.hasLength(phone, "手机已被注册");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.isEquals(password, rpassword, "密码需要一致");

        //String key = "VerifyCode:" + phone;
        //使用枚举
        String key = RedisKeys.VERIFY_CODE.join(phone);
        String code = userInfoRedisService.getVerifyCode(key);
        if (code == null || !code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码失效或错误");
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setLevel(1); //级别
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfo.setPassword(password);
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) throws LogicException{
        //1.获取到请求参数
        //2.通过参数查询是否存在用户,若不存在抛出异常
        UserInfo user = userInfoMapper.selecyByUsernameAndPassword(username, password);
        if (user == null) {
            throw new LogicException("账号或密码错误");
        }
        //3.创建一个token值, token值作为key, user 作为value, 并设置过时时间 30min
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        String key = RedisKeys.USER_LOGIN.join(token);
        userInfoRedisService.setToken(key, user);
        return key;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    @Override
    public void updateById(UserInfo gserInfo) {
        userInfoMapper.updateByPrimaryKey(gserInfo);
    }

    @Override
    public void updateHearderById(String hearderImg, Long id) {
        userInfoMapper.updateHearderById(hearderImg,id);
    }

    @Override
    public String getUser(String phone) {
        UserInfo user = userInfoMapper.selectUserByPhone(phone);
        //3.创建一个token值, token值作为key, user 作为value, 并设置过时时间 30min
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        String key = RedisKeys.USER_LOGIN.join(token);
        userInfoRedisService.setToken(key, user);
        return key;
    }
    @Override
    public void updatePhone(String phone, String newMobile) {
        List<String> allPhone = userInfoMapper.getAllPhone();
        if (allPhone.contains(newMobile)){
            throw new LogicException("该手机号已经被绑定");
        }
        userInfoMapper.updatePhone(phone,newMobile);
    }

    /*@Override
    public void updatePhone(String phone, String newMobile) {
        List<String> allPhone = userInfoMapper.getAllPhone();
        if (allPhone.contains(newMobile)){
            throw new LogicException("该手机号已经被绑定");
        }
        userInfoMapper.updatePhone(phone,newMobile);
    }*/


}
